package com.ssu.judea.travlog.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.json.JSONObject;

import statics.TravlogVars;

import com.ssu.judea.travlog.model.AndroidDAO;
import com.ssu.judea.travlog.vo.Member;
import com.ssu.judea.travlog.vo.Result;

import comm.util.RequestManager;
import comm.util.TravlogHashMap;

public class AndroidServlet extends TravlogServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Logger logger = Logger.getLogger(AndroidServlet.class);
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		super.service(request, response);
		TravlogHashMap hm = RequestManager.getRequest(request);
		
		AndroidDAO dao = new AndroidDAO();
		
		if(hm==null || hm.get("ACTION")==null || hm.get("ACTION").toString().length()<1){
			response.getWriter().write("Please request valid");
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("auth")){
			dao.toAuthpage(request, response);
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("toTwitter")){
			dao.toTwitterPage(request, response);
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("callback")){
			try {
				Member result = dao.authorizeFromMobile(request,response);
				
				if(result.getMemberseq()>0){
					request.setAttribute("AUTH_RESULT", TravlogVars.SUCCESS);
					request.setAttribute("MEMBER", result);
					
					logger.debug("인증완료"+result.getMemberseq());
				}else{
					request.setAttribute("AUTH_RESULT", TravlogVars.FAIL);
				}
				
				request.getRequestDispatcher("/jsp/android/authresult.jsp").forward(request, response);
			} catch (ParserConfigurationException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("checkAuth")){
			Member checkMem = dao.getMemberBySession(request.getSession().getId());
			if(checkMem!=null)
				request.setAttribute("CURRENT_MEMBER", checkMem);
			request.getRequestDispatcher("/jsp/android/checkauth.jsp").forward(request, response);
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("getCurrentSession")){
			request.getRequestDispatcher("/jsp/android/returnCurrentSessionId.jsp").forward(request, response);
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("imageUpload")){
			TravlogHashMap resultHm = dao.fileUpload(request);
			if(resultHm!=null && resultHm.get("UPLOAD_FILE").toString().lastIndexOf(".")>1){
				response.getWriter().write(resultHm.get("UPLOAD_FILE").toString());
			}else{
				response.getWriter().write(TravlogVars.FAIL);
			}
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("writePost")){
			Result result = dao.writePost(hm);
			response.getWriter().write((new JSONObject(result)).toString());
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("logout")){
			request.getSession().invalidate();
			response.getWriter().write(TravlogVars.SUCCESS);
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("getUserPostDateList")){
			if(hm.get("SEQ")!=null){
				String result = dao.getUserPostDateList(hm.get("SEQ").toString());
				response.setCharacterEncoding("UTF-8");
				response.getWriter().write(result);
			}else{
				response.getWriter().write(TravlogVars.FAIL);
			}
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("getUserPostByDate")){
			if(hm.get("SEQ")!=null && hm.get("DATE")!=null){
				response.setCharacterEncoding("UTF-8");
				response.getWriter().write(dao.getUserPostByDate("http://"+request.getServerName()+request.getContextPath(),hm.get("SEQ").toString(),hm.get("DATE").toString()));
			}else{
				response.getWriter().write(TravlogVars.FAIL);
			}
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("getRecentOpenPosts")){
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(dao.getRecentOpenPosts());
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("getFollowersPosts")){
			response.setCharacterEncoding("UTF-8");
			response.getWriter().write(dao.getFollowersList(hm.get("SEQ").toString()));
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("follow")){
			response.setCharacterEncoding("UTF-8");
			Result result = new Result();
			String resultStr = dao.doFollow(hm.get("TARGET").toString(), hm.get("SEQ").toString());
			result.setResult(resultStr);
			if(resultStr.equals(TravlogVars.FAIL)){
				result.setMessage("Follow fail, or already followed.");
			}else{
				result.setMessage("Followed.");
			}
			response.getWriter().write(result.toJSONString());
		}else if(hm.get("ACTION")!=null && hm.get("ACTION").toString().equals("mention")){
			response.setCharacterEncoding("UTF-8");
			Result result = new Result();
			String resultStr = dao.doWriteMention(hm.get("TEXT").toString(),hm.get("SEQ").toString());
			result.setResult(resultStr);
			if(resultStr.equals(TravlogVars.FAIL)){
				result.setMessage("Twitter Posting Fail.");
			}else{
				result.setMessage("Posted.");
			}
			response.getWriter().write(result.toJSONString());
		}
	}
}
